Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates add-on to ember-cli 2.16 #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rauhryan
Copy link

@rauhryan rauhryan commented Mar 3, 2018

This project uses semver, please check the scope of this pr:

  • #none# - documentation fixes and/or test additions
  • #patch# - backwards-compatible bug fix
  • #minor# - adding functionality in a backwards-compatible manner
  • #major# - incompatible API change

CHANGELOG

Accepting this PR will upgrade the add-on to the latest version of ember-cli making it compatible with babel 6+

I deserve no credit, I simply merged and tested @Dhaulagiri branch, works great and fixed a deploy issue for us on heroku

@Dhaulagiri
Copy link

Niiiice, thanks for resurrecting this. I don't even remember why I never opened this PR 🤷‍♂️, but I was just looking at this again because of the babel 5 dep so it would be awesome to get this landed. I noticed a few minor nitpicks with what I did 4 months ago but I could clean those up in a separate PR.

"redux": "3.5.2",
"redux-thunk": "2.1.0",
"svg4everybody": "2.1.0"
"svg4everybody": "2.1.0",
"yarn": "^1.5.1"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did this fix the heroku issue you were referring to?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we were getting this error on the heroku dyno build output

Build Error (Babel) in spin.js
The plugin function (_ref) {
    var t = _ref.types;
    /**
     * Add a helper to take an initial descriptor, apply some decorators to it, and optionally
     * define the property.
     */
    function ensureApplyDecoratedDescriptorHelper(path, state) {
        if (!state.applyDecoratedDescriptor) {
            state.applyDecoratedDescriptor = path.scope.generateUidIdentifier('applyDecoratedDescriptor');
            var helper = buildApplyDecoratedDescriptor({
                NAME: state.applyDecoratedDescriptor
            });
            path.scope.getProgramParent().path.unshiftContainer('body', helper);
        }
        return state.applyDecoratedDescriptor;
    }
    /**
     * Add a helper to call as a replacement for class property definition.
     */
    function ensureInitializerDefineProp(path, state) {
        if (!state.initializerDefineProp) {
            state.initializerDefineProp = path.scope.generateUidIdentifier('initDefineProp');
            var helper = buildInitializerDefineProperty({
                NAME: state.initializerDefineProp
            });
            path.scope.getProgramParent().path.unshiftContainer('body', helper);
        }
        return state.initializerDefineProp;
    }
    /**
     * Add a helper that will throw a useful error if the transform fails to detect the class
     * property assignment, so users know something failed.
     */
    function ensureInitializerWarning(path, state) {
        if (!state.initializerWarningHelper) {
            state.initializerWarningHelper = path.scope.generateUidIdentifier('initializerWarningHelper');
            var helper = buildInitializerWarningHelper({
                NAME: state.initializerWarningHelper
            });
            path.scope.getProgramParent().path.unshiftContainer('body', helper);
        }
        return state.initializerWarningHelper;
    }
    /**
     * If the decorator expressions are non-identifiers, hoist them to before the class so we can be sure
     * that they are evaluated in order.
     */
    function applyEnsureOrdering(path) {
        // TODO: This should probably also hoist computed properties.
        var decorators = (path.isClass() ? [path].concat(path.get('body.body')) : Error running: ember build --environment production 2>&1
path.get('properties')).reduce(function (acc, prop) {
            return acc.concat(prop.node.decorators || []);
        }, []);
        var identDecorators = decorators.filter(function (decorator) {
            return !t.isIdentifier(decorator.expression);
        });
        if (identDecorators.length === 0) return;
        return t.sequenceExpression(identDecorators.map(function (decorator) {
            var expression = decorator.expression;
            var id = decorator.expression = path.scope.generateDeclaredUidIdentifier('dec');
            return t.assignmentExpression('=', id, expression);
        }).concat([path.node]));
    }
    /**
     * Given a class expression with class-level decorators, create a new expression
     * with the proper decorated behavior.
     */
    function applyClassDecorators(classPath, state) {
        var decorators = classPath.node.decorators || [];
        classPath.node.decorators = null;
        if (decorators.length === 0) return;
        var name = classPath.scope.generateDeclaredUidIdentifier('class');
        return decorators.map(function (dec) {
            return dec.expression;
        }).reverse().reduce(function (acc, decorator) {
            return buildClassDecorator({
                CLASS_REF: name,
                DECORATOR: decorator,
                INNER: acc
            }).expression;
        }, classPath.node);
    }
    /**
     * Given a class expression with method-level decorators, create a new expression
     * with the proper decorated behavior.
     */
    function applyMethodDecorators(path, state) {
        var hasMethodDecorators = path.node.body.body.some(function (node) {
            return (node.decorators || []).length > 0;
        });
        if (!hasMethodDecorators) return;
        return applyTargetDecorators(path, state, path.node.body.body);
    }
    /**
     * Given an object expression with property decorators, create a new expression
     * with the proper decorated behavior.
     */
    function applyObjectDecorators(path, state) {
        var hasMethodDecorators = path.node.properties.some(function (node) {
            return (node.decorators || []).length > 0;
        });
        if (!hasMethodDecorators) return;
        return applyTargetDecorators(path, state, path.node.properties);
    }
    /**
     * A helper to pull out property decorators into a sequence expression.
     */
    function applyTargetDecorators(path, state, decoratedProps) {
        var descName = path.scope.generateDeclaredUidIdentifier('desc');
        var valueTemp = path.scope.generateDeclaredUidIdentifier('value');
        var name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? 'class' : 'obj');
        var exprs = decoratedProps.reduce(function (acc, node) {
            var decorators = node.decorators || [];
            node.decorators = null;
            if (decorators.length === 0) return acc;
            if (node.computed) {
                throw path.buildCodeFrameError('Computed method/property decorators are not yet supported.');
            }
            var property = t.isLiteral(node.key) ? node.key : t.stringLiteral(node.key.name);
            var target = path.isClass() && !node.static ? buildClassPrototype({
                CLASS_REF: name
            }).expression : name;
            if (t.isClassProperty(node, { static: false })) {
                var descriptor = path.scope.generateDeclaredUidIdentifier('descriptor');
                var initializer = node.value ? t.functionExpression(null, [], t.blockStatement([t.returnStatement(node.value)])) : t.nullLiteral();
                node.value = t.callExpression(ensureInitializerWarning(path, state), [descriptor, t.thisExpression()]);
                acc = acc.concat([t.assignmentExpression('=', descriptor, t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [target, property, t.arrayExpression(decorators.map(function (dec) {
                    return dec.expression;
                })), t.objectExpression([t.objectProperty(t.identifier('enumerable'), t.booleanLiteral(true)), t.objectProperty(t.identifier('initializer'), initializer)])]))]);
            } else {
                acc = acc.concat(t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [target, property, t.arrayExpression(decorators.map(function (dec) {
                    return dec.expression;
                })), t.isObjectProperty(node) || t.isClassProperty(node, { static: true }) ? buildGetObjectInitializer({
                    TEMP: path.scope.generateDeclaredUidIdentifier('init'),
                    TARGET: target,
                    PROPERTY: property
                }).expression : buildGetDescriptor({
                    TARGET: target,
                    PROPERTY: property
                }).expression, target]));
            }
            return acc;
        }, []);
        return t.sequenceExpression([t.assignmentExpression('=', name, path.node), t.sequenceExpression(exprs), name]);
    }
    return {
        inherits: require("babel-plugin-syntax-decorators"),
        visitor: {
            ExportDefaultDeclaration: function ExportDefaultDeclaration(path) {
                if (!path.get("declaration").isClassDeclaration()) return;
                var node = path.node;
                var ref = node.declaration.id || path.scope.generateUidIdentifier("default");
                node.declaration.id = ref;
                // Split the class declaration and the export into two separate statements.
                path.replaceWith(node.declaration);
                path.insertAfter(t.exportNamedDeclaration(null, [t.exportSpecifier(ref, t.identifier('default'))]));
            },
            ClassDeclaration: function ClassDeclaration(path) {
                var node = path.node;
                var ref = node.id || path.scope.generateUidIdentifier("class");
                path.replaceWith(t.variableDeclaration("let", [t.variableDeclarator(ref, t.toExpression(node))]));
            },
            ClassExpression: function ClassExpression(path, state) {
                // Create a replacement for the class node if there is one. We do one pass to replace classes with
                // class decorators, and a second pass to process method decorators.
                var decoratedClass = applyEnsureOrdering(path) || applyClassDecorators(path, state) || applyMethodDecorators(path, state);
                if (decoratedClass) path.replaceWith(decoratedClass);
            },
            ObjectExpression: function ObjectExpression(path, state) {
                var decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state);
                if (decoratedObject) path.replaceWith(decoratedObject);
            },
            AssignmentExpression: function AssignmentExpression(path, state) {
                if (!state.initializerWarningHelper) return;
                if (!path.get('left').isMemberExpression()) return;
                if (!path.get('left.property').isIdentifier()) return;
                if (!path.get('right').isCallExpression()) return;
                if (!path.get('right.callee').isIdentifier({ name: state.initializerWarningHelper.name })) return;
                path.replaceWith(t.callExpression(ensureInitializerDefineProp(path, state), [path.get('left.object').node, t.stringLiteral(path.get('left.property').node.name), path.get('right.arguments')[0].node, path.get('right.arguments')[1].node]));
            }
        }
    };
} didn't export a Plugin instance

but never had any issues locally, I merged in your branch and switched over to a GitHub dependency on our fork and everything is working great

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it had something to do with babel 5, sometimes nodejs is mystery

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I have built many an ember app on Heroku and I can't say I've ever seen this 🤷‍♂️ or needed to add yarn as a direct dep. What buildpack(s) are you using to build the app?

@rauhryan
Copy link
Author

rauhryan commented Mar 3, 2018

Adding the build log error from heroku, so maybe someone will get some google juice from it and it will make them happy 😄

Build Error (Babel) in spin.js
The plugin function (_ref) {
    var t = _ref.types;
    /**
     * Add a helper to take an initial descriptor, apply some decorators to it, and optionally
     * define the property.
     */
    function ensureApplyDecoratedDescriptorHelper(path, state) {
        if (!state.applyDecoratedDescriptor) {
            state.applyDecoratedDescriptor = path.scope.generateUidIdentifier('applyDecoratedDescriptor');
            var helper = buildApplyDecoratedDescriptor({
                NAME: state.applyDecoratedDescriptor
            });
            path.scope.getProgramParent().path.unshiftContainer('body', helper);
        }
        return state.applyDecoratedDescriptor;
    }
    /**
     * Add a helper to call as a replacement for class property definition.
     */
    function ensureInitializerDefineProp(path, state) {
        if (!state.initializerDefineProp) {
            state.initializerDefineProp = path.scope.generateUidIdentifier('initDefineProp');
            var helper = buildInitializerDefineProperty({
                NAME: state.initializerDefineProp
            });
            path.scope.getProgramParent().path.unshiftContainer('body', helper);
        }
        return state.initializerDefineProp;
    }
    /**
     * Add a helper that will throw a useful error if the transform fails to detect the class
     * property assignment, so users know something failed.
     */
    function ensureInitializerWarning(path, state) {
        if (!state.initializerWarningHelper) {
            state.initializerWarningHelper = path.scope.generateUidIdentifier('initializerWarningHelper');
            var helper = buildInitializerWarningHelper({
                NAME: state.initializerWarningHelper
            });
            path.scope.getProgramParent().path.unshiftContainer('body', helper);
        }
        return state.initializerWarningHelper;
    }
    /**
     * If the decorator expressions are non-identifiers, hoist them to before the class so we can be sure
     * that they are evaluated in order.
     */
    function applyEnsureOrdering(path) {
        // TODO: This should probably also hoist computed properties.
        var decorators = (path.isClass() ? [path].concat(path.get('body.body')) : Error running: ember build --environment production 2>&1
path.get('properties')).reduce(function (acc, prop) {
            return acc.concat(prop.node.decorators || []);
        }, []);
        var identDecorators = decorators.filter(function (decorator) {
            return !t.isIdentifier(decorator.expression);
        });
        if (identDecorators.length === 0) return;
        return t.sequenceExpression(identDecorators.map(function (decorator) {
            var expression = decorator.expression;
            var id = decorator.expression = path.scope.generateDeclaredUidIdentifier('dec');
            return t.assignmentExpression('=', id, expression);
        }).concat([path.node]));
    }
    /**
     * Given a class expression with class-level decorators, create a new expression
     * with the proper decorated behavior.
     */
    function applyClassDecorators(classPath, state) {
        var decorators = classPath.node.decorators || [];
        classPath.node.decorators = null;
        if (decorators.length === 0) return;
        var name = classPath.scope.generateDeclaredUidIdentifier('class');
        return decorators.map(function (dec) {
            return dec.expression;
        }).reverse().reduce(function (acc, decorator) {
            return buildClassDecorator({
                CLASS_REF: name,
                DECORATOR: decorator,
                INNER: acc
            }).expression;
        }, classPath.node);
    }
    /**
     * Given a class expression with method-level decorators, create a new expression
     * with the proper decorated behavior.
     */
    function applyMethodDecorators(path, state) {
        var hasMethodDecorators = path.node.body.body.some(function (node) {
            return (node.decorators || []).length > 0;
        });
        if (!hasMethodDecorators) return;
        return applyTargetDecorators(path, state, path.node.body.body);
    }
    /**
     * Given an object expression with property decorators, create a new expression
     * with the proper decorated behavior.
     */
    function applyObjectDecorators(path, state) {
        var hasMethodDecorators = path.node.properties.some(function (node) {
            return (node.decorators || []).length > 0;
        });
        if (!hasMethodDecorators) return;
        return applyTargetDecorators(path, state, path.node.properties);
    }
    /**
     * A helper to pull out property decorators into a sequence expression.
     */
    function applyTargetDecorators(path, state, decoratedProps) {
        var descName = path.scope.generateDeclaredUidIdentifier('desc');
        var valueTemp = path.scope.generateDeclaredUidIdentifier('value');
        var name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? 'class' : 'obj');
        var exprs = decoratedProps.reduce(function (acc, node) {
            var decorators = node.decorators || [];
            node.decorators = null;
            if (decorators.length === 0) return acc;
            if (node.computed) {
                throw path.buildCodeFrameError('Computed method/property decorators are not yet supported.');
            }
            var property = t.isLiteral(node.key) ? node.key : t.stringLiteral(node.key.name);
            var target = path.isClass() && !node.static ? buildClassPrototype({
                CLASS_REF: name
            }).expression : name;
            if (t.isClassProperty(node, { static: false })) {
                var descriptor = path.scope.generateDeclaredUidIdentifier('descriptor');
                var initializer = node.value ? t.functionExpression(null, [], t.blockStatement([t.returnStatement(node.value)])) : t.nullLiteral();
                node.value = t.callExpression(ensureInitializerWarning(path, state), [descriptor, t.thisExpression()]);
                acc = acc.concat([t.assignmentExpression('=', descriptor, t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [target, property, t.arrayExpression(decorators.map(function (dec) {
                    return dec.expression;
                })), t.objectExpression([t.objectProperty(t.identifier('enumerable'), t.booleanLiteral(true)), t.objectProperty(t.identifier('initializer'), initializer)])]))]);
            } else {
                acc = acc.concat(t.callExpression(ensureApplyDecoratedDescriptorHelper(path, state), [target, property, t.arrayExpression(decorators.map(function (dec) {
                    return dec.expression;
                })), t.isObjectProperty(node) || t.isClassProperty(node, { static: true }) ? buildGetObjectInitializer({
                    TEMP: path.scope.generateDeclaredUidIdentifier('init'),
                    TARGET: target,
                    PROPERTY: property
                }).expression : buildGetDescriptor({
                    TARGET: target,
                    PROPERTY: property
                }).expression, target]));
            }
            return acc;
        }, []);
        return t.sequenceExpression([t.assignmentExpression('=', name, path.node), t.sequenceExpression(exprs), name]);
    }
    return {
        inherits: require("babel-plugin-syntax-decorators"),
        visitor: {
            ExportDefaultDeclaration: function ExportDefaultDeclaration(path) {
                if (!path.get("declaration").isClassDeclaration()) return;
                var node = path.node;
                var ref = node.declaration.id || path.scope.generateUidIdentifier("default");
                node.declaration.id = ref;
                // Split the class declaration and the export into two separate statements.
                path.replaceWith(node.declaration);
                path.insertAfter(t.exportNamedDeclaration(null, [t.exportSpecifier(ref, t.identifier('default'))]));
            },
            ClassDeclaration: function ClassDeclaration(path) {
                var node = path.node;
                var ref = node.id || path.scope.generateUidIdentifier("class");
                path.replaceWith(t.variableDeclaration("let", [t.variableDeclarator(ref, t.toExpression(node))]));
            },
            ClassExpression: function ClassExpression(path, state) {
                // Create a replacement for the class node if there is one. We do one pass to replace classes with
                // class decorators, and a second pass to process method decorators.
                var decoratedClass = applyEnsureOrdering(path) || applyClassDecorators(path, state) || applyMethodDecorators(path, state);
                if (decoratedClass) path.replaceWith(decoratedClass);
            },
            ObjectExpression: function ObjectExpression(path, state) {
                var decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state);
                if (decoratedObject) path.replaceWith(decoratedObject);
            },
            AssignmentExpression: function AssignmentExpression(path, state) {
                if (!state.initializerWarningHelper) return;
                if (!path.get('left').isMemberExpression()) return;
                if (!path.get('left.property').isIdentifier()) return;
                if (!path.get('right').isCallExpression()) return;
                if (!path.get('right.callee').isIdentifier({ name: state.initializerWarningHelper.name })) return;
                path.replaceWith(t.callExpression(ensureInitializerDefineProp(path, state), [path.get('left.object').node, t.stringLiteral(path.get('left.property').node.name), path.get('right.arguments')[0].node, path.get('right.arguments')[1].node]));
            }
        }
    };
} didn't export a Plugin instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants